Page({
  data: {
    items: [{
      name: "李百",
      userType: "儿童",
      userCord: "3111 2589 8512 365 222",
      phone: "180 0011 1166",
      aditIcon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1554730335957&di=1e9837ba2d57962420431cff000231ed&imgtype=0&src=http%3A%2F%2Fs9.rr.itc.cn%2Fr%2FwapChange%2F201612_1_15%2Fa6bhk0669360644330.jpg',
      id: 1,
    }, {
      name: "李涛",
      userType: "成人",
      userCord: "3211 2589 8512 365 222",
      phone: "155 0011 1166",
        aditIcon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555327366&di=a399b5909f1aeac6c22a665f14747c08&imgtype=jpg&er=1&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201804%2F29%2F20180429162017_nyzfa.thumb.700_0.jpg',
      id: 18,
    }, {
      name: "红桃",
      userType: "成人",
      userCord: "3711 2589 8512 365 222",
      phone: "150 0011 2166",
        aditIcon: 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=31277262,3146729331&fm=26&gp=0.jpg',
      id: 17,
    }, {
      name: "李丽丽",
      userType: "成人",
      userCord: "3711 2589 8512 365 222",
      phone: "199 0011 2166",
      id: 16,
        aditIcon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555327639&di=8476682241c522bb96116e3df2d6d2d1&imgtype=jpg&er=1&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180808%2Fef3ca7a96fc0455f826cf017c92ab3b7.jpeg',
    }, {
      name: "李莉莉",
      userType: "成人",
      userCord: "3711 2589 8512 365 222",
      phone: "199 0011 2166",
      id: 15,
        aditIcon: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=629473949,2726878460&fm=26&gp=0.jpg',
    }, {
      name: "李丽里",
      userType: "成人",
      userCord: "3711 2589 8512 365 222",
      phone: "199 0011 2166",
      id: 14,
        aditIcon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1554732988680&di=45e4e7076d2c5cbb7c8dfba5ff51ca24&imgtype=0&src=http%3A%2F%2Fwww.qqtu8.com%2Ff%2F20120729170703.gif',
    }, {
      name: "朱丽君",
      userType: "成人",
      userCord: "3711 2589 8512 365 222",
      phone: "199 0011 2166",
      id: 13,
        aditIcon: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2163897570,2076997604&fm=26&gp=0.jpg',
    }, {
      name: "朱翔",
      userType: "成人",
      userCord: "3711 2589 8512 365 222",
      phone: "199 0011 2166",
      id: 11,
        aditIcon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1554733063609&di=4d8ca91e9c3bf029131279e9e77fb9f8&imgtype=0&src=http%3A%2F%2F88.2295.com%2Fq%2F2010-9-18%2F0ebc33ddc07682bea23117b57cfaedaf.gif',
    }, {
      name: "沈瀚",
      userType: "成人",
      userCord: "3711 2589 8512 365 222",
      phone: "199 0011 2166",
      id: 12,
        aditIcon: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555327796&di=e0838afe14a09e5a17fdfb37df16f1b7&imgtype=jpg&er=1&src=http%3A%2F%2Fwenwen.soso.com%2Fp%2F20120127%2F20120127165713-1051940942.jpg',
    }],
    startX: 0,
    startY: 0,
    mengbanBol: true
  },
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: "左划删除"
    })
  },
  touchstart: function(e) {
    //开始触摸时 重置所有删除
    this.data.items.forEach(function(v, i) {
      if (v.isTouchMove) //只操作为true的
        v.isTouchMove = false;
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      items: this.data.items
    })
  },
  touchmove: function(e) {
    var that = this,
      index = e.currentTarget.dataset.index, //当前索引
      startX = that.data.startX, //开始X坐标
      startY = that.data.startY, //开始Y坐标
      touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
      //获取滑动角度
      angle = that.angle({
        X: startX,
        Y: startY
      }, {
        X: touchMoveX,
        Y: touchMoveY
      });
    that.data.items.forEach(function(v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX) //右滑
          v.isTouchMove = false
        else {//左滑
          v.isTouchMove = true
        } 
      }
    })
    //更新数据
    that.setData({
      items: that.data.items
    })
  },
  /**

* 计算滑动角度
* @param {Object} start 起点坐标
* @param {Object} end 终点坐标
*/
  angle: function(start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  //删除事件
  del: function(e) {
    console.log(`发送请求,删除id为${e.currentTarget.dataset.id}的项目`)
    this.data.items.splice(e.currentTarget.dataset.index, 1)
    this.setData({
      items: this.data.items
    })
  }
})